<?php


/**
 * Class Action
 *
 * 批量生成设备编号
 */
class Action
{

    public function index() {

        $is_last = Request::Get('last', 0);
        $is_create = Request::Get('create', 0);
        $is_excel = Request::Get('excel', 0);

        $TYPES = device_config::$TYPES;
        $RULES = device_config::$RULES;

        // 返回的数据
        $data = array();
        $last_file = '';

        if ($is_create) {

            $TYPE = Request::getParam('TYPE');
            $CLASS = Request::getParam('CLASS');
            $NUM = Request::getParam('num', 0);
            $SB_ID = Request::getParam('SB_ID', -1);

            if ($TYPE && $NUM > 0 && $_SESSION['SB_ID'] == $SB_ID) {
                // 生成
                $_type = $TYPES[$TYPE];
                $FINDER = $_type['prefix'];

                $VALUES = array();
                foreach ($RULES[$CLASS] as $rule_id => $rule) {
                    if (is_array($rule)) {
                        $_v = Request::getParam($CLASS . '_' . $rule_id);
                        $FINDER .= $_v;
                        $VALUES[] = $_v . '|' . $rule[1];
                    } else {
                        $VALUES[] = '::' . $rule_id . '|' . $rule;
                    }
                }

                // 取得相同规格的列表
                $existList = _model('device')->getFields('code', array("code LIKE " => $FINDER . '%'));

                // 排重生成, 直到数量够
                $newList = array();
                while (count($newList) < $NUM) {
                    $_tmp = $_type['prefix'] . call_user_func_array(array('device_helper', 'code_generate'), $VALUES);
                    if (in_array($_tmp, $existList)) {
                        continue;
                    }
                    $newList[] = $_tmp;
                }

                // 写文件
                $file_path = ROOT_PATH . '/export';
                $last_file = $file_path . '/device_last.csv';
                $unquire_file = $file_path . '/device_' . $_type['prefix'] . '_' . $NUM . '_' . date('YmdHis') . '.csv';
                if (!is_dir($file_path)) {
                    mkdir($file_path, 0777);
                }
                file_put_contents($last_file, join("\n", $newList));
                file_put_contents($unquire_file, join("\n", $newList));

                $dt = datetime();
                foreach ($newList as $code) {
                    _model('device')->create(array("type" => $_type['prefix'], 'code' => $code, 'add_time' => $dt));
                }

                $data = $newList;

                unset($_SESSION['SB_ID']);

            } else {
                Response::redirect('device/admin/create');
            }
        } elseif ($is_last) {
            $file_path = ROOT_PATH . '/export';
            $last_file = $file_path . '/device_last.csv';
            if (is_readable($last_file)) {
                $data = file($last_file);
            }
        } elseif ($is_excel) {
            $file_path = ROOT_PATH . '/export';
            $last_file = $file_path . '/device_last.csv';
            if (is_readable($last_file)) {
                $data = file_get_contents($last_file);
            }
            header("Content-Type: application/force-download");
            header("Content-Type: application/download");
            header("Content-Type: application/octet-stream");

            $title = '自动生成设备导出' . datetime() . ".csv";
            // ff正常，ie不正常，下面是从网站找到的解决方案
            if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) {
                $title = rawurlencode($title);
            }
            header("Content-Disposition: attachment; filename=" . $title);
            header("Content-Transfer-Encoding: binary");
            //最后修改时间
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
            //不使用缓存
            header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
            header('Cache-Control: public');
            header("Expires: 0");
            header("Pragma: public");
            exit($data);
        }

        if (!$is_create) {
            $_SESSION['SB_ID'] = $SB_ID = mt_rand(1000, 9999);
        }

        Response::assign('TYPES', $TYPES);
        Response::assign('RULES', $RULES);
        Response::assign('TYPES_CONFIG', json_encode($TYPES));
        Response::assign('RULES_CONFIG', json_encode($RULES));
        Response::assign('SB_ID', $SB_ID);
        Response::assign('data', $data);
        Response::display('admin/create.html');
    }
}
